package com.tanhua.sso.server.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.tanhua.result.Result;
import com.tanhua.sso.server.service.SmallVideosService;
import com.tanhua.sso.vo.VideoVo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("smallVideos")
public class SmallVideosController {
    @Autowired
    private SmallVideosService smallVideosService;

    /*上传视频*/
    @PostMapping
    public ResponseEntity smallVideos(MultipartFile videoThumbnail, MultipartFile videoFile){
        smallVideosService.smallVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /*视频列表*/
    //开启缓存
    @GetMapping
    public ResponseEntity selectSmallVideos(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pagesize){
        Result result =smallVideosService.selectSmallVideos(page,pagesize);
        return ResponseEntity.ok(result);
    }

    //关注视频作者
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable Long uid){
        smallVideosService.userFocus(uid);
        return ResponseEntity.ok(null);
    }

    //取消用户关注
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable Long uid){
        smallVideosService.userUnFocus(uid);
        return ResponseEntity.ok(null);
    }

    /*视频点赞*/
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable ObjectId id){
        smallVideosService.like(id);
        return ResponseEntity.ok(null);
    }

    /*取消点赞*/
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable ObjectId id){
        smallVideosService.dislike(id);
        return ResponseEntity.ok(null);
    }


    /*评论列表*/
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pagesize,@PathVariable ObjectId id){
        Result result=smallVideosService.commentsPage(page,pagesize,id);
        return ResponseEntity.ok(result);
    }

    /*发布评论*/
    @PostMapping("/{id}/comments")
    public ResponseEntity sendComments(@PathVariable ObjectId id, @RequestBody Map map){
        String comment = (String) map.get("comment");
        smallVideosService.sendComments(id,comment);
        return ResponseEntity.ok(null);
    }
}
